﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace AwesomeGame.Screens.Controls
{
    public class PlayerList
    {
        private List<Player> _players;
        private Vector2 _position;
        private SpriteFont _font;
        private bool _visible;

        public PlayerList(Vector2 position)
        {
            _position = position;
            _players = new List<Player>();
            _font = Game1.ContentManager.Load<SpriteFont>(@"Fonts\candc16pt");
            _visible = true;
        }

        public void AddPlayer(Player player)
        {
            _players.Add(player);
        }

        public void ChangePlayerScore(long id, int scoreChange)
        {
            for (int i = _players.Count - 1; i >= 0; i--)
                if (_players[i].Id == id)
                {
                    _players[i].Score += scoreChange;
                    break;
                }
        }

        public void Clear()
        {
            _players.Clear();
        }

        public void RemovePlayer(Player player)
        {
            _players.Remove(player);
        }

        public string GetPlayerNameFromId(long id)
        {
            for (int i = _players.Count - 1; i >= 0; i--)
                if (_players[i].Id == id)
                    return _players[i].Name;
            return "";
        }

        public void ToggleDisplay()
        {
            _visible = !_visible;
        }

        public void Update(GameTime gameTime)
        {
        }

        public void Draw(GameTime gameTime)
        {
            int c = 0;
            if(_visible)
                for (int i = _players.Count - 1; i >= 0; i--)
                {
                    Game1.SpriteBatch.DrawString(_font, _players[i].Name + ": " + _players[i].Score, new Vector2(_position.X, _position.Y + (c * 15)), Color.Red);
                    c += 1;
                }
        }
    }
}
